﻿@page "/VerficaManage"
@using Rj.PersonnelVerification.Client.Models
@using Rj.PersonnelVerification.Shared.Models.Request
@using Rj.PersonnelVerification.Shared.Models.Response
@inject IDialogService DialogService
<h3 class="p-4">人证补传</h3>

<MudGrid Spacing="6" Justify="Justify.Center" class="pl-4 pr-4">
    <MudItem md="8" class="align-content-center">
        <MudStack Row="true">
        <MudTextField @bind-Value="uploadRequest.HospNo" Label="住院号" Variant="Variant.Outlined" Margin="Margin.Dense" class=""/>
        <MudDateRangePicker Label ="入院日期" @bind-DateRange="@_dateRange" Margin="Margin.Dense" ReadOnly="false" Clearable="true" 
                            PlaceholderStart="开始日期" PlaceholderEnd="结束日期"/>
        </MudStack>
    </MudItem>
    <MudItem md="4" class="align-content-center">
        <MudButton Variant="Variant.Filled" Color="Color.Info" Disabled="@isLoading" OnClick="QueryNoUpload">未上传记录查询</MudButton>
        <MudButton Variant="Variant.Filled" Color="Color.Info" Disabled="@isLoading" OnClick="Upload">上传</MudButton>
    </MudItem>
    <MudItem md="12">
        <MudDataGrid T="QueryNoUploadResponse" MultiSelection="true" Selection="SelectionMode.Multiple" Items="@uploadResponses" SortMode="SortMode.Multiple" 
        Filterable="true" QuickFilter="@_quickFilter"
            Hideable="true" @bind-SelectedItems="SelectedPploadResponses" class="p-4" ReadOnly="true">
        <ToolBarContent>
            <MudText Typo="Typo.h6">未上传数据</MudText>
            <MudSpacer />
            <MudTextField @bind-Value="_searchString" Placeholder="输入住院号\卡号进行过滤..." Adornment="Adornment.Start" Immediate="true"
                AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" Class="mt-0"></MudTextField>
        </ToolBarContent>
        <Columns>
            <SelectColumn T="QueryNoUploadResponse" />
            <PropertyColumn Property="x => x.RegNo" Title="住院流水号" />
            <PropertyColumn Property="x => x.HospNo"  Title="住院号"/>
            <PropertyColumn Property="x => x.CardNo"  Title="卡号"/>
            <PropertyColumn Property="x => x.PatId" Title="患者ID"/>
            <PropertyColumn Property="x => x.PatName" Title="患者姓名" />
        </Columns>
        <PagerContent>
            <MudDataGridPager T="QueryNoUploadResponse" />
        </PagerContent> 
        </MudDataGrid>
        @if (isLoading)
        {
            <div style="position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(255, 255, 255, 0.7); display: flex; justify-content: center; align-items: center;">
                <MudProgressCircular Indeterminate="true" Size="Size.Large" Color="Color.Info" />
            </div>
        }
    </MudItem>
</MudGrid>




@code {
    private bool isLoading = false;
    [Inject]
    public QueryNoUploadRequest uploadRequest{ get; set; }

    private IEnumerable<QueryNoUploadResponse> uploadResponses;
    private HashSet<QueryNoUploadResponse> SelectedPploadResponses=new();
    [Inject]
    public IHttpClientFactory httpClientFactory { get; set; }
    [Inject]
    public ISnackbar Snackbar{ get; set; }

    private DateRange _dateRange { get; set; }

    public string HospNo { get; set; }
    private string _searchString;


    public async Task QueryNoUpload()
    {
        isLoading = true;
        if (HospNo == null && (_dateRange == null || _dateRange.Start == null))
        {
            Snackbar.Add("请填写住院号或者选择日期", Severity.Error);
            isLoading = false;
            return;
        }
        StateHasChanged();
        var httpClient=httpClientFactory.CreateClient("ApiClient");
        uploadRequest.BeginTime = _dateRange.Start?.Date.AddHours(-12);
        uploadRequest.EndTime = _dateRange.End?.Date.AddHours(12);
        var response = await httpClient.PostAsJsonAsync("api/Verification/QueryByExample", uploadRequest);
        if (!response.IsSuccessStatusCode)
        {
            isLoading = false;
            StateHasChanged();
            return;
        }
        uploadResponses = await response.Content.ReadFromJsonAsync<IEnumerable<QueryNoUploadResponse>>();
        isLoading = false;
        StateHasChanged();
    }

    private Func<QueryNoUploadResponse, bool> _quickFilter => x =>
    {
        if (string.IsNullOrWhiteSpace(_searchString))
            return true;

        if (x.PatName.Contains(_searchString, StringComparison.OrdinalIgnoreCase))
            return true;

        if (x.HospNo.Contains(_searchString, StringComparison.OrdinalIgnoreCase))
            return true;

        if ($"{x.CardNo}".Contains(_searchString))
            return true;

        return false;
    };

    private async Task Upload()
    {
        isLoading = true;
        DialogOptions _dialogOptions = new() { FullWidth = true, DisableBackdropClick = true };
        var parameters = new DialogParameters<DialogUsageExample_Dialog> { { x => x.SelectedPploadResponses, SelectedPploadResponses.ToList() } };
        var dialog = await DialogService.ShowAsync<DialogUsageExample_Dialog>("Delete Server", parameters, _dialogOptions);
        var result = await dialog.Result;
        QueryNoUpload();
    }
}
